package cn.lcfms.app;

import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
public class BaseController {
	/**
	 * 该方法将在执行控制器之前被执行
	 */
    protected void beforeProtected(HttpServletRequest request,HttpServletResponse response){
 	
    }  
    /**
   	 * 该方法将在执行控制器之后，调入模板之前被执行
     * @throws IOException 
   	 */
    protected void afterProtected(HttpServletRequest request,HttpServletResponse response,ModelAndView view) throws IOException{
    	String contextPath = request.getContextPath();
	    String basePath = "";
	    if(request.getServerPort()==80){
	    	basePath = request.getScheme()+"://"+request.getServerName()+contextPath+"/";
	    }else{
	    	basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+contextPath+"/";
	    }	
	    request.setAttribute("APP", basePath); 	   
    	if(view!=null && view.getViewName()!=null){	
    		String viewname=view.getViewName();	
			if(viewname.startsWith("[") && viewname.endsWith("]")){
				response.setContentType("application/json;charset=utf-8");
				response.getWriter().print(viewname);
				response.flushBuffer();
				view.clear();
			}
			if(viewname.startsWith("{") && viewname.endsWith("}")){
				response.setContentType("application/json;charset=utf-8");	
				response.getWriter().print(viewname);
				response.flushBuffer();
				view.clear();
			}
		}
    }
}
